from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout, 
                            QPushButton, QLabel, QLineEdit, QTabWidget, 
                            QTableView, QComboBox, QGroupBox)
from PyQt6.QtCore import Qt
import sys

class StyledWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QSS样式表示例")
        self.resize(600, 400)
        
        # 主窗口部件
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        
        # 主布局
        layout = QVBoxLayout()
        central_widget.setLayout(layout)
        
        # 创建控件
        self.create_basic_controls(layout)
        self.create_tab_widget(layout)
        
        # 应用样式表
        self.apply_stylesheet()
    
    def create_basic_controls(self, layout):
        """创建基本控件"""
        group = QGroupBox("基本控件")
        group_layout = QVBoxLayout()
        group.setLayout(group_layout)
        
        # 标签
        self.label = QLabel("这是一个样式化的标签")
        
        # 按钮
        self.button = QPushButton("普通按钮")
        self.button.setObjectName("normalButton")
        
        self.hover_button = QPushButton("悬停效果按钮")
        self.hover_button.setObjectName("hoverButton")
        
        # 输入框
        self.line_edit = QLineEdit()
        self.line_edit.setPlaceholderText("请输入内容...")
        
        # 组合框
        self.combo_box = QComboBox()
        self.combo_box.addItems(["选项1", "选项2", "选项3"])
        
        # 添加到布局
        group_layout.addWidget(self.label)
        group_layout.addWidget(self.button)
        group_layout.addWidget(self.hover_button)
        group_layout.addWidget(self.line_edit)
        group_layout.addWidget(self.combo_box)
        
        layout.addWidget(group)
    
    def create_tab_widget(self, layout):
        """创建标签页控件"""
        self.tab_widget = QTabWidget()
        
        # 第一个标签页
        tab1 = QWidget()
        tab1_layout = QVBoxLayout()
        tab1.setLayout(tab1_layout)
        
        self.table_view = QTableView()
        tab1_layout.addWidget(self.table_view)
        
        # 第二个标签页
        tab2 = QWidget()
        tab2_layout = QVBoxLayout()
        tab2.setLayout(tab2_layout)
        
        self.login_button = QPushButton("登录")
        self.login_button.setObjectName("loginButton")
        
        tab2_layout.addWidget(QLabel("登录表单"))
        tab2_layout.addWidget(QLineEdit(placeholderText="用户名"))
        tab2_layout.addWidget(QLineEdit(placeholderText="密码", echoMode=QLineEdit.EchoMode.Password))
        tab2_layout.addWidget(self.login_button)
        
        # 添加标签页
        self.tab_widget.addTab(tab1, "数据表格")
        self.tab_widget.addTab(tab2, "登录")
        
        layout.addWidget(self.tab_widget)
    
    def apply_stylesheet(self):
        """应用样式表"""
        stylesheet = """
        /* 全局样式 */
        * {
            font-family: "Microsoft YaHei";
            font-size: 14px;
        }
        
        QWidget {
            background-color: #f5f5f5;
            color: #333333;
        }
        
        /* 按钮样式 */
        QPushButton {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            min-width: 80px;
        }
        
        QPushButton:hover {
            background-color: #45a049;
        }
        
        QPushButton:pressed {
            background-color: #367c39;
        }
        
        /* 特殊按钮 */
        #hoverButton {
            background-color: #2196F3;
        }
        
        #hoverButton:hover {
            background-color: #0b7dda;
        }
        
        #loginButton {
            background-color: #ff5722;
        }
        
        #loginButton:hover {
            background-color: #e64a19;
        }
        
        /* 输入框样式 */
        QLineEdit {
            padding: 6px;
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            background: white;
        }
        
        QLineEdit:focus {
            border: 1px solid #4CAF50;
        }
        
        /* 标签样式 */
        QLabel {
            font-size: 16px;
            color: #333333;
        }
        
        /* 组合框样式 */
        QComboBox {
            padding: 3px;
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            background: white;
        }
        
        QComboBox::drop-down {
            width: 20px;
            border-left: 1px solid #d9d9d9;
        }
        
        /* 分组框样式 */
        QGroupBox {
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            margin-top: 10px;
            padding-top: 15px;
        }
        
        QGroupBox::title {
            subcontrol-origin: margin;
            left: 10px;
            padding: 0 3px;
        }
        
        /* 标签页样式 */
        QTabWidget::pane {
            border: 1px solid #d9d9d9;
            border-radius: 4px;
            margin-top: -1px;
        }
        
        QTabBar::tab {
            background: #f0f0f0;
            padding: 8px 16px;
            border: 1px solid #d9d9d9;
            border-bottom: none;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
        }
        
        QTabBar::tab:selected {
            background: #ffffff;
            border-bottom-color: #ffffff;
        }
        
        /* 表格样式 */
        QTableView {
            gridline-color: #e0e0e0;
            alternate-background-color: #f9f9f9;
            background: white;
            border: 1px solid #d9d9d9;
        }
        
        QHeaderView::section {
            background-color: #f0f0f0;
            padding: 5px;
            border: none;
            border-bottom: 1px solid #d9d9d9;
        }
        """
        
        self.setStyleSheet(stylesheet)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    
    # 设置应用程序样式为Fusion，以获得更好的视觉效果
    app.setStyle("Fusion")
    
    window = StyledWindow()
    window.show()
    
    sys.exit(app.exec())